package com.molyfun.parents.view.zhibo;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;

import com.molyfun.parents.zhibo.im.ImModel;

import java.util.List;

/**
 * Created by william(liuchao.hit@gmail.com).
 * 如果聊天列表滑动后，5s内不自动滚动
 */
public class ZhiboImListView extends RecyclerView {

    protected LayoutInflater layoutInflater;

    private Context context;

    LinearLayoutManager mLinearLayoutManager;
    private ZhiboImAdapter mZhiboImAdapter;

    private static final int FLAG_RESUME_SCROLL = 1001;

    private static final int RESUME_SCROLL_TIME_INTERVAL = 5000;

    public int mCurrentZhiboState = ZhiboImListState.StateListNormal;

    public interface ZhiboImListState{
        int StateListNormal = 1001;
        int StateListScroll = 1002;
    }

    public ZhiboImListView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public ZhiboImListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ZhiboImListView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        mLinearLayoutManager = new LinearLayoutManager(context);
//        mLinearLayoutManager.setStackFromEnd(true);
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        setLayoutManager(mLinearLayoutManager);
        setHasFixedSize(true);
    }

    @Override
    public void onScrollStateChanged(int state) {
        if(state == SCROLL_STATE_DRAGGING){
            mCurrentZhiboState = ZhiboImListState.StateListScroll;
            mHandler.removeMessages(FLAG_RESUME_SCROLL);
            mHandler.sendEmptyMessageDelayed(FLAG_RESUME_SCROLL, 5000);
        }
        super.onScrollStateChanged(state);
    }

    public void initData(String tips){
        mZhiboImAdapter = new ZhiboImAdapter(context, tips);
        setAdapter(mZhiboImAdapter);
    }

    public void addMsg(ImModel imModel){
        mZhiboImAdapter.addMsg(imModel);
        if(mZhiboImAdapter.getItemCount() > 0 && mCurrentZhiboState == ZhiboImListState.StateListNormal){
            smoothScrollToPosition(mZhiboImAdapter.getItemCount() - 1);
        }
    }

    /**
     * 当seek后刷新数据
     * @param imModels
     */
    public void refreshData(List<ImModel> imModels){
        mZhiboImAdapter.refreshData(imModels);
    }

    Handler mHandler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case FLAG_RESUME_SCROLL:
                    mCurrentZhiboState = ZhiboImListState.StateListNormal;
                    if(mZhiboImAdapter != null && mZhiboImAdapter.getItemCount() > 0){
                        smoothScrollToPosition(mZhiboImAdapter.getItemCount() - 1);
                    }
                    break;
            }
        }
    };

}